// pages/gerenxinxi/gerenxinxi.js
var utils = require('../../utils/api.js');
var base64 = require('../../utils/base64.js');
var md5 = require('../../utils/md5.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isSetAddress: false,
    isShowName: false,
    isPhoneShow: false,
    region: ['选择省份', '选择城市', '选择区'], //省市区
    name: null, //姓名
    phone: null, //电话
    address: null, //地址
    code: null, //手机验证
    buttonDisable: false, //验证码按钮关闭
    verifyCodeTime: '获取验证码', //验证码显示
    sendSMSTime: 60, //发短信按钮等待时间
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  tipbgtap: function () {
    this.setData({
      isSetAddress: false,
      isShowName: false,
      isPhoneShow: false
    })
  },
  addresstap: function () {
    this.setData({
      isSetAddress: true
    })
  },
  setname: function () {
    this.setData({
      isShowName: true
    })
  },
  setphone: function () {
    this.setData({
      isPhoneShow: true
    })
  },
  /**
   * 保存信息
   */
  saveInfo: function () {
    var that = this
    //保存地址信息判断
    //手机号码
    if (!that.data.phone) {
      that.CommonShowMsg('请输入您的手机号码')
      return false
    } else {
      var regMobile = /^1\d{10}$/;
      if (!regMobile.test(that.data.phone)) {
        that.CommonShowMsg('请输入正确的手机号')
        return false
      }
    }
    //验证码
    if (!that.data.code) {
      that.CommonShowMsg('请输入您的验证码')
      return false
    }
    //姓名
    if (!that.data.name) {
      that.CommonShowMsg('请输入您的姓名')
      return false
    }
    if (that.data.region[0] == '选择省份') {
      that.CommonShowMsg('请您选择省市区')
      return false
    }
    if (!that.data.address) {
      that.CommonShowMsg('请输入您的详细地址')
      return false
    }
    // console.log('name:' + that.data.name + ' phone:' + that.data.phone + ' address:' + that.data.region + ',' + that.data.address)
    wx.showModal({
      title: '提示',
      content: '您确认要保存?',
      success: function (res) {
        if (res.confirm) {
          that.setPersonInfoReq()
        } else if (res.cancel) {
        }
      }
    })

  },
  /**
   * 获取电话
   */
  getPhone: function (e) {
    var that = this
    that.setData({ phone: e.detail.value })
  },
  /**
   * 获取姓名
   */
  getName: function (e) {
    var that = this
    that.setData({ name: e.detail.value })
  },
  /**
   * 获取地址
   */
  getAddress: function (e) {
    var that = this
    that.setData({ address: e.detail.value })
  },
  getCode: function (e) {
    var that = this
    that.setData({ code: e.detail.value })
  },
  /**
   * 获取验证码req
   */
  getCodeReq: function () {
    var that = this
    if (!that.data.phone) {
      wx.showToast({
        title: '请输入您的手机号码',
        icon: 'none',
      })
      return false
    } else {
      var regMobile = /^1\d{10}$/;
      if (!regMobile.test(that.data.phone)) {
        wx.showToast({
          title: '手机号有误！',
          icon: 'none',
        })
        return false
      }
    }
    //发送验证码开始
    if (that.data.buttonDisable) return false;
    var c = that.data.sendSMSTime;
    var intervalId = setInterval(function () {
      c = c - 1;
      that.setData({
        verifyCodeTime: c + 's后重发',
        buttonDisable: true,
      })
      if (c == 0) {
        clearInterval(intervalId);
        that.setData({
          verifyCodeTime: '获取验证码',
          buttonDisable: false,
        })
      }
    }, 1000)
    that.sendVerifyCodeReq();//获取短信验证码接口
  },
  /**
   * 发送验证码req
   */
  sendVerifyCodeReq: function () {
    var that = this
    let phone = that.data.phone
    wx.request({
      url: getApp().globalData.requestUrl + '/getIdentifyingCode',
      method: "post",
      data: {
        paramList: base64.CusBASE64.encoder('{"phoneNumber":"' + phone + '"}')
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // console.log(res)
        if (res.statusCode == 200) { //判断wx.request请求是否成功
          if (res.data) {
            var dat = utils.stringToJson(utils.ascii2native(res.data))
            console.log('interface:getIdentifyingCode')
            console.log(dat)
            if (dat.status == 0) {
              that.CommonShowMsg('短信已发送')
            } else {
              that.CommonShowMsg(dat.errorMsg)
            }
          } else {//服务器没有返回数据
            that.CommonShowMsg('请求数据失败')
          }
        } else {//请求失败
          that.CommonShowMsg('请求数据失败')
        }
      },
      fail: function (res) {
      },
      complete: function () {
      },
    })
  },
  /**
   * 错误公用信息输出
   */
  CommonShowMsg: function (Msg) {
    wx.showToast({
      title: Msg,
      mask: true,
      icon: 'none',
      duration: 3000,
    })
  },
  /**
   * 提交保存的位置信息req
   */
  setPersonInfoReq: function () {
    var that = this
    wx.request({
      url: getApp().globalData.requestUrl + '/updateUserInfo',
      method: "post",
      data: {
        paramList: base64.CusBASE64.encoder('{"serverSessionKey":"' + wx.getStorageSync('serverSessionKey') + '","phoneNumber":"' + that.data.phone + '","code":"' + that.data.code + '","name":"' + that.data.name + '","address":"' + that.data.address + '"}')
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //console.log(res)
        if (res.statusCode == 200) { //判断wx.request请求是否成功
          if (res.data) {
            var dat = utils.stringToJson(utils.ascii2native(res.data))
            console.log('interface:updateUserInfo')
            console.log(dat)
            if (dat.status == 0) {
              that.CommonShowMsg('保存成功')
            } else {
              that.CommonShowMsg(dat.errorMsg)
            }
          } else {//服务器没有返回数据
            that.CommonShowMsg('请求数据失败')
          }
        } else {//请求失败
          that.CommonShowMsg('请求数据失败')
        }
      },
      fail: function (res) {
      },
      complete: function () {
      },
    })
  }
})